﻿using System;
using System.Text;
using System.Collections.Generic;

namespace ImageDownloadClient
{
/*    class DownloadController
    {
        LogAppend WritelnLog;

        public enum DownloadStatus { STATUS_STOP, STATUS_PAUSE, STATUS_WORKING };
        public enum IMAGE_SEARCH { GOOGLE_IMAGE_SEARCH };
        public DownloadController(LogAppend la, int nMaxThread, DownloadLog downloadLog)
        {
            m_nMaxThread        = nMaxThread;
            m_downloadLog       = downloadLog;
            WritelnLog = la;

            // 初始化下载线程
            m_listThreads = new List<DownloadThread>();
            for (int i = 0; i < nMaxThread; i++)
            {
                DownloadThread newThread = new DownloadThread(la, downloadLog, this);
                m_listThreads.Add(newThread);
            }

            // 初始化状态
            m_DownloadStatus = DownloadStatus.STATUS_STOP;
        }

        public void start(String imageFolder, int maxDownload)
        {
            m_imageFolder   = imageFolder;
            m_nMaxDownload  = maxDownload;
            m_nThreadNum = maxDownload;

            // 开始
            for (int i = 0; i < maxDownload; i++)
            {
                m_listThreads[i].start();
            }
           // m_listThreads[0].start();

            // 设置开始时间
            m_downloadLog.setStartTime(DateTime.Now);

            // 启动完所有线程后，设置状态
            m_DownloadStatus = DownloadStatus.STATUS_WORKING;
        }

        public void pause()
        {
            for (int i = 0; i < m_listThreads.Count; i++)
            if(m_listThreads[i] != null)
            {
                m_listThreads[i].pause();
            }

            // 停止或暂停后，累计操作时间
            m_downloadLog.addMoreTime(DateTime.Now);

            // 暂停完所有线程后，设置状态
            m_DownloadStatus = DownloadStatus.STATUS_PAUSE;
        }

        public void stop()
        {
            for (int i = 0; i < m_listThreads.Count; i++)
            if (m_listThreads[i] != null)
            {
                m_listThreads[i].stop();
            }

            // 停止或暂停后，累计操作时间
            m_downloadLog.addMoreTime(DateTime.Now);

            // 停止完所有线程后，设置状态
            m_DownloadStatus = DownloadStatus.STATUS_STOP;
        }

        public void resume()
        {
            for (int i = 0; i < m_listThreads.Count; i++)
            if (m_listThreads[i] != null)
            {
                m_listThreads[i].resume();
            }

            // 设置开始时间
            m_downloadLog.setStartTime(DateTime.Now);

            // 启动完所有线程后，设置状态
            m_DownloadStatus = DownloadStatus.STATUS_WORKING;
        }

        // 是否所有下载线程都不处于下载状态，
        // 主要是GetResponse()函数
        public bool allThreadNotDownloading()
        {
            bool result = true;
            for (int i = 0; i < m_listThreads.Count; i++)
            if (m_listThreads[i] != null)
            {
                if (m_listThreads[i].isDownloading())
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        public DownloadStatus getCurStatus()
        {
            return m_DownloadStatus;
        }

        public DownloadLog getDownloadLog()
        {
            return m_downloadLog;
        }

        private int                     m_nThreadNum;
        private int                     m_nMaxThread;
        private List<DownloadThread>    m_listThreads;
        private String                  m_imageFolder;
        private int                     m_nMaxDownload;
        private DownloadLog             m_downloadLog;
        private DownloadStatus          m_DownloadStatus;
    }*/
}
